package jm.data.structure.leet_code;

import jm.data.structure.set.Set;

import java.util.TreeSet;

/**
 * @Description https://leetcode-cn.com/problems/intersection-of-two-arrays/
 * @date 2022/4/23 15:27
 */
public class _349两个数组的交集 {

    public static void main(String[] args) {
        int[] nums1 = {1,2,2,1};
        int[] nums2 = {2,2};
        int[] intersection = intersection(nums1, nums2);
        for (int i : intersection) {
            System.out.println(i);
        }
    }

    public static int[] intersection(int[] nums1, int[] nums2) {
        TreeSet<Integer> set = new TreeSet<>();
        TreeSet<Integer> set2 = new TreeSet<>();

        for (int i = 0; i < nums1.length; i++) {
            set.add(nums1[i]);
        }
        for (int i = 0; i < nums2.length; i++) {
            // 如果已经存在就是 “交集”
            if (set.contains(nums2[i])){
                set2.add(nums2[i]);
            }
        }
        int[] arr = new int[set2.size()];
        int i = 0;
        for (Integer integer : set2) {
            arr[i] = integer;
            i++;
        }
        return arr;
    }
}


